<?php
/**
 * Created by PhpStorm.
 * BuyUser: USER
 * Date: 2018/11/12
 * Time: 11:24
 */

namespace common\sources\write\buyuser;
use common\models\craftsman\BuyUser;
use Yii;

class BuyUserAccess implements IBuyUserAccess
{
    public function addBuyUser($data)
    {
        $insert = 'INSERT INTO buyuser (phone,password,qq,wx,reg_time)
                   VALUES (:phone,:password,:qq,:wx,:reg_time)';
        $res = Yii::$app->db->createCommand($insert )->bindValues([
            ':phone' => $data['phone'],
            ':password'=>$data['password'],
            ':qq'=>$data['qq'],
            ':wx'=>$data['wx'],
            ':reg_time'=>$data['reg_time']
        ])->execute();
        return $res;
    }

    public function UpdatePwd($data)
    {
        $sql = 'UPDATE ' . BuyUser::tableName() . ' SET password = :password,update_time = :update_time WHERE phone = :phone ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':password' => $data['password'],
                ':update_time' => time(),
                ':phone' => $data['phone']
            ])->execute();

    }
    public function modifyQQWX($buy_id,$update)
    {
        if(!empty($update['qq'])){
            $sql = 'UPDATE ' . BuyUser::tableName() . ' SET qq = :qq,update_time = :update_time WHERE id = :id ';
            return Yii::$app->db->createCommand($sql)
                ->bindValues([
                    ':qq' => $update['qq'],
                    ':update_time' => time(),
                    ':id' => $buy_id
                ])->execute();
        }
        if(!empty($update['wx'])){
            $sql = 'UPDATE ' . BuyUser::tableName() . ' SET wx = :wx,update_time = :update_time WHERE id = :id ';
            return Yii::$app->db->createCommand($sql)
                ->bindValues([
                    ':wx' => $update['wx'],
                    ':update_time' => time(),
                    ':id' => $buy_id
                ])->execute();
        }
        return false;
    }
    public function setPayPwd($buy_id,$update)
    {
        $sql = 'UPDATE ' . BuyUser::tableName() . ' SET paypwd = :paypwd,update_time = :update_time WHERE id = :id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':paypwd' => $update['paypwd'],
                ':update_time' => time(),
                ':id' => $buy_id
            ])->execute();
    }
    //修改手机号码
    public function modifyPhone($buyId,$phone){
        $sql = 'UPDATE ' . BuyUser::tableName() . ' SET phone = :phone,update_time = :update_time WHERE id = :id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':phone' => $phone,
                ':update_time' => time(),
                ':id' => $buyId
            ])->execute();
    }

    //添加黑名单
    public function AddBlackUser($data) {
        $user = BuyUser::findOne(['id' => $data['userId']]);
        if ($user) {
            $user->freeze = $data['freeze_status'];
            $user->freeze_reason = $data['freeze_reason'];
            $user->freeze_time = date("Y-m-d H:i:s",time());
            $user->freeze_reason = $data['freeze_operator'];
            if ($user->save()) {
                return true;
            }
        }
        return false;
    }
}